import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		appId: 'wx177c8d050fa174bd',
		code: null,
		isLoginStatus: false, // 是否登录
		userInfo: {},
		timerIdent: false,//全局1s定时器，只在全局开启一个，所有需要定时执行的任务监听该值即可，无需额外开启
		tabbar: [{
				iconPath: "/static/tabbar/wd.png",
				selectedIconPath: "/static/tabbar/wd_active.png",
				text: '气温',
				pagePath: "/pages/index"
			},
			{
				iconPath: "/static/tabbar/add.png",
				selectedIconPath: "/static/tabbar/add.png",
				text: '',
				midButton: true,
				pagePath: "/pages/addRecord"
			},
			{
				iconPath: "/static/tabbar/up.png",
				selectedIconPath: "/static/tabbar/up_active.png",
				text: '需冷量',
				pagePath: "/pages/needCode"
			},
		]
	},
	getters: {
		hasLogin(state){
			return !!state.isLoginStatus;
		}
	},
	mutations: {
		//更新state数据
		setStateAttr(state, param){
			if(param instanceof Array){
				for(let item of param){
					state[item.key] = item.val;
				}
			}else{
				state[param.key] = param.val;
			}
		},
		setUserInfo(state, data) {
			state.userInfo = data
			state.isLoginStatus = true
		},
		clearUserInfo(state) {
			state.userInfo = {}
			state.isLoginStatus = false
		},
		//更新token
		setToken(state, data){
			const {token, tokenExpired} = data;
			state.token = token;
			uni.setStorageSync('uniIdToken', token);
			uni.setStorageSync('tokenExpired', tokenExpired);
		},
		//退出登录
		logout(state){
			state.token = '';
			uni.removeStorageSync('uniIdToken');
			setTimeout(()=>{
				state.userInfo = {};
			}, 1100)
		},
		
		setCode(state, code) {
			state.code = code
		}
	},
	actions: {
	}
}) 


export default store